package com.twu.biblioteca;

import java.io.IOException;
import java.util.List;

/**
 * Created by sahelig on 19/06/14.
 */
public class ListMovies implements MenuAction
{
        private Library libraryOnWhichOperationsArePerformed;
        private String stringToBeReturned;
        private boolean successful;
        private String actionPerformed;

        public ListMovies(Library lib)
        {

            libraryOnWhichOperationsArePerformed = lib;
            actionPerformed ="to List Movies";
        }


        public Result performAction(UserInterface ui,String libraryNumber)throws IOException
        {


            List<Movie> availableMovies = libraryOnWhichOperationsArePerformed.getAvailableMovies();

            int len = availableMovies.size();

            if (len == 0)
            {
                ui.display("No Movies in library at present");
                stringToBeReturned = "No Movies in library at present";
                successful = false;
            }
            else
            {
                successful = true;
                stringToBeReturned="Movies are available in the library";
                ui.display("The available movies are:");
                for (int i = 0; i < len; i++)
                {
                    ui.displayMovie(availableMovies.get(i));

                }
            }

            return new Result(successful, stringToBeReturned);
        }

        public String toString()
        {
            return actionPerformed;
        }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ListMovies that = (ListMovies) o;

        if (successful != that.successful) return false;
        if (actionPerformed != null ? !actionPerformed.equals(that.actionPerformed) : that.actionPerformed != null)
            return false;
        if (libraryOnWhichOperationsArePerformed != null ? !libraryOnWhichOperationsArePerformed.equals(that.libraryOnWhichOperationsArePerformed) : that.libraryOnWhichOperationsArePerformed != null)
            return false;
        if (stringToBeReturned != null ? !stringToBeReturned.equals(that.stringToBeReturned) : that.stringToBeReturned != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = libraryOnWhichOperationsArePerformed != null ? libraryOnWhichOperationsArePerformed.hashCode() : 0;
        result = 31 * result + (stringToBeReturned != null ? stringToBeReturned.hashCode() : 0);
        result = 31 * result + (successful ? 1 : 0);
        result = 31 * result + (actionPerformed != null ? actionPerformed.hashCode() : 0);
        return result;
    }
}
